// pages/login/login.js
import http from"../../utils/http"
import Toast from"@vant/weapp/toast/toast"

Page({

  /**
   * 页面的初始数据
   */
  data: {
   form:{
    username:"201711010001",
    password:"",
    userType:1
   },
   usernameMsg:"",
   passwordMsg:""
  },

  //修改用户名
  usernameInput(e){
    console.log(e);
    this.setData({
     " form.username":e.detail
    })
  },
  //修改密码
  passwordInput(e){
    this.setData({
      "form.password":e.detail
    })
  },
  //验证
  validate(){
    let v=true;
    if(!this.data.form.username){
      v=false
      this.setData({
        usernameMsg:"请填写账号"
      })
    }else{
      this.setData({
        usernameMsg:""
      })
    }
    if(!this.data.form.password){
      v=false
      this.setData({
        passwordMsg:"请填写密码"
      })
    }else{
      this.setData({
        passwordMsg:""
      })
    }
    return v
  },
//提
  onSubmit(){
    console.log(this.data.form);
    if(this.validate()){
      //网络请求
      http({
        url:"/user/login",
        method:"post",
        data:this.data.form
      }).then(res=>{
        console.log(res);
        if(res.code===0){
          Toast({
            type:"success",
            message:"登录成功",
            onClose:()=>{
              console.log("执行onclose函数");
              //存信息
              wx.setStorageSync('userInfo',res.data)
              // 跳转
              wx.switchTab({
                url: '/pages/search/search',
              })
            },
          });
        }else{
          //轻提示
          Toast.fail(res.message);
        }
      }).catch(err=>{
        console.log(err,1);
       Toast.fail('失败文案');
      })
    }
  }
})
